package com.example.st.service.impl;

import com.example.st.common.ResponseResult;
import com.example.st.dao.InfoRepository;
import com.example.st.entity.Info;
import com.example.st.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class InfoServiceImpl implements InfoService {

    @Autowired
    private InfoRepository infoRepository;

    @Override
    public ResponseResult<List<Info>> getAllInfo() {
        List<Info> infos = infoRepository.findAll();
        return new ResponseResult<>(200, "Success", infos);
    }

    @Override
    public ResponseResult<Info> getInfoById(Long id) {
        Optional<Info> infoOptional = infoRepository.findById(id);
        if (infoOptional.isPresent()) {
            return new ResponseResult<>(200, "Success", infoOptional.get());
        } else {
            return new ResponseResult<>(404, "Info not found", null);
        }
    }

    @Override
    public ResponseResult<Info> createInfo(Info info) {
        info.setStatus(1); // Set status as active initially
        info.setTime(LocalDateTime.now());
        Info createdInfo = infoRepository.save(info);
        return new ResponseResult<>(200, "Info created successfully", createdInfo);
    }

    @Override
    public ResponseResult<Info> updateInfo(Long id, Info infoDetails) {
        Optional<Info> optionalInfo = infoRepository.findById(id);
        if (optionalInfo.isPresent()) {
            Info existingInfo = optionalInfo.get();
            existingInfo.setAddress(infoDetails.getAddress());
            existingInfo.setEvent(infoDetails.getEvent());
            existingInfo.setTime(infoDetails.getTime());
            existingInfo.setDuration(infoDetails.getDuration());
            // Keep the existing status
            existingInfo.setStatus(existingInfo.getStatus());
            Info updatedInfo = infoRepository.save(existingInfo);
            return new ResponseResult<>(200, "Info updated successfully", updatedInfo);
        } else {
            return new ResponseResult<>(404, "Info not found", null);
        }
    }

    @Override
    public ResponseResult<Void> invalidateInfo(Long id) {
        Optional<Info> optionalInfo = infoRepository.findById(id);
        if (optionalInfo.isPresent()) {
            Info existingInfo = optionalInfo.get();
            existingInfo.setStatus(0); // Set status as inactive
            infoRepository.save(existingInfo);
            return new ResponseResult<>(200, "Info invalidated successfully", null);
        } else {
            return new ResponseResult<>(404, "Info not found", null);
        }
    }

    @Override
    public ResponseResult<Void> activateInfo(Long id) {
        Optional<Info> optionalInfo = infoRepository.findById(id);
        if (optionalInfo.isPresent()) {
            Info existingInfo = optionalInfo.get();
            existingInfo.setStatus(1); // Set status as active
            infoRepository.save(existingInfo);
            return new ResponseResult<>(200, "Info activated successfully", null);
        } else {
            return new ResponseResult<>(404, "Info not found", null);
        }
    }
}

